package com.spring.demos.ioccontainer;

import com.alibaba.druid.pool.DruidDataSource;
import com.spring.demos.ioccontainer.configuration.AppConfig;
import com.spring.demos.ioccontainer.pojo.*;
import com.spring.demos.ioccontainer.service.FixedDepositService;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

import java.util.*;

public class Spring5IocTest {
    private static ApplicationContext applicationContext;
    @BeforeClass
    public static void init(){
//           applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        // applicationContext = new FileSystemXmlApplicationContext("D:\\SpringBootProject\\spring5Demos\\src\\main\\resources\\beans.xml");
         applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
    }
    @Test
    public void xmlConfigBeanTest() {

         ExampleBeanA exampleBeanA = (ExampleBeanA) applicationContext.getBean("exampleBeanA");

         System.out.println(exampleBeanA);

         ExampleBeanB exampleBeanB = (ExampleBeanB) applicationContext.getBean("exampleBeanB");

         System.out.println(exampleBeanB);

         Car car = (Car) applicationContext.getBean("car");

         car.showCarType();

         System.out.println(car);

         PhoneFactory phoneFactory = (PhoneFactory) applicationContext.getBean("phoneFactory");

         System.out.println(phoneFactory);

         Phone phone = (Phone) applicationContext.getBean("phone");

         phone.sayPhoneType();

         System.out.println(phone);


    }

    @Test
    public void dependencyInjectTest(){
        ExampleBean exampleBean = applicationContext.getBean(ExampleBean.class);
        System.out.println(exampleBean);
        SimpleMovieListener simpleMovieListener = applicationContext.getBean(SimpleMovieListener.class);
        if(simpleMovieListener!=null){
            simpleMovieListener.onFindMovie("泰坦尼克号");
            simpleMovieListener.onFindMovie("肖申克的救赎");
        }
        EmailMessageSender emailMessageSender = applicationContext.getBean(EmailMessageSender.class);
        if(emailMessageSender!=null){
            emailMessageSender.sendText("zhangsan@163.com","lisi@163.com","wangwu@163.com","今天是2020年2月12日");
        }

    }

    @Test
    public void complexPropertiesTest(){

        ComplexBean complexBean = applicationContext.getBean(ComplexBean.class);

        Properties properties = complexBean.getProperties();

        System.out.println(properties.getProperty("jdbc.driver.className"));

        System.out.println(properties.getProperty("jdbc.url"));

        Properties adminEmails = complexBean.getAdminEmails();

        System.out.println(adminEmails.getProperty("administrator"));

        System.out.println(adminEmails.getProperty("support"));

        System.out.println(adminEmails.getProperty("development"));

        List<String> favorites = complexBean.getFavorites();

        for(String favorite: favorites){
            System.out.println(favorite);
        }

        List<Object> classInstances = complexBean.getClassInstances();

        System.out.println(classInstances.getClass().getName());

        System.out.println(classInstances.get(0));

        Car car = (Car) classInstances.get(1);

        car.showCarType();

        Phone phone = (Phone) classInstances.get(2);

        phone.sayPhoneType();

        Set<Object> unRepeatedInstance = complexBean.getUnRepeatInstance();

        System.out.println(unRepeatedInstance.getClass().getName());
        Iterator<Object> iterator = unRepeatedInstance.iterator();
        while (iterator.hasNext()){
            Object element = iterator.next();
            if(element instanceof MovieFinder){
                MovieFinder movieFinder = (MovieFinder) element;
                movieFinder.callFindMovie("中国机长");
            }else{
                System.out.println(element);
            }
        }

        Map<String,Object> caches = complexBean.getCaches();

        Set<String> keys = caches.keySet();

        for(String key: keys){
            Object value = caches.get(key);
            if(value instanceof Car){
                Car car1 = (Car) value;
                car1.showCarType();
            }else if(value instanceof Phone){
                Phone phone1 = (Phone) value;
                phone1.sayPhoneType();
            }else{
                System.out.println(value);
            }
        }
    }

    @Test
    public void placeholderTest(){
        PropertySourcesPlaceholderConfigurer placeholderConfigurer = applicationContext.getBean(PropertySourcesPlaceholderConfigurer.class);
        System.out.println(placeholderConfigurer);
        DruidDataSource druidDataSource = applicationContext.getBean(DruidDataSource.class);
        if(druidDataSource!=null){
            String driverClassName = druidDataSource.getDriverClassName();
            String username = druidDataSource.getUsername();
            String connectUrl = druidDataSource.getUrl();
            int maxActive = druidDataSource.getMaxActive();
            System.out.println("driverClassName:"+driverClassName);
            System.out.println("username:"+username);
            System.out.println("connectUrl:"+connectUrl);
            System.out.println("maxActive:"+maxActive);
            System.out.println("password:"+druidDataSource.getPassword());
        }
    }

    @Test
    public void scopeTest(){
       ExampleBeanA exampleBeanA1 = applicationContext.getBean(ExampleBeanA.class);

       ExampleBeanA exampleBeanA2 = applicationContext.getBean(ExampleBeanA.class);

        System.out.println("exampleBeanA1==exampleBeanA2:"+(exampleBeanA1==exampleBeanA2));

        ExampleBeanB exampleBeanB1 = applicationContext.getBean(ExampleBeanB.class);

        ExampleBeanB exampleBeanB2 = applicationContext.getBean(ExampleBeanB.class);

        System.out.println("exampleBeanB1==exampleBeanB2:"+(exampleBeanB1==exampleBeanB2));

    }

    @Test
    public void jdbcInsertTest(){

        FixedDepositDetails fixedDepositDetails1 = new FixedDepositDetails("62221001","张三",12000.0f,
                "zhangsan@163.com","2020-02-13 19:00:00","x_heshengfu");

        FixedDepositDetails fixedDepositDetails2 = new FixedDepositDetails("62221002","李四",11000.0f,
                "lisi@163.com","2020-02-13 19:00:00","x_heshengfu");

        FixedDepositDetails fixedDepositDetails3 = new FixedDepositDetails("62221003","王五",11800.0f,
                "wangwu@163.com","2020-02-13 19:00:00","x_heshengfu");

        FixedDepositDetails fixedDepositDetails4 = new FixedDepositDetails("62221004","钱六",11500.0f,
                "qiainliu@163.com","2020-02-13 19:00:00","x_heshengfu");

        FixedDepositDetails fixedDepositDetails5 = new FixedDepositDetails("62221005","赵七",10500.0f,
                "zhaoqi@163.com","2020-02-13 19:00:00","x_heshengfu");

        FixedDepositDetails fixedDepositDetails6 = new FixedDepositDetails("62221006","晓雪",10400.0f,
                "xiaoxue@163.com","2020-02-13 19:00:00","x_heshengfu");

        List<FixedDepositDetails> fixedDepositDetailsList = new ArrayList<>();

        fixedDepositDetailsList.add(fixedDepositDetails2);

        fixedDepositDetailsList.add(fixedDepositDetails3);

        fixedDepositDetailsList.add(fixedDepositDetails4);

        fixedDepositDetailsList.add(fixedDepositDetails5);

        fixedDepositDetailsList.add(fixedDepositDetails6);

        FixedDepositService fixedDepositService = applicationContext.getBean(FixedDepositService.class);

        fixedDepositService.saveDepositDetail(fixedDepositDetails1);

        fixedDepositService.batchSaveDepositDetails(fixedDepositDetailsList);

    }

    @Test
    public void annotationConfigTest(){

        FixedDepositService fixedDepositService = applicationContext.getBean(FixedDepositService.class);

        FixedDepositDetails fixedDepositDetails1 = fixedDepositService.queryDepositById("62221001");

        System.out.println(fixedDepositDetails1.toString());

        fixedDepositService.saveMoney("62221001",1000.0f);

        FixedDepositDetails fixedDepositDetails2 = fixedDepositService.queryDepositById("62221001");

        System.out.println(fixedDepositDetails2);

        FixedDepositDetails fixedDepositDetails3 = fixedDepositService.queryDepositById("62221002");

        System.out.println(fixedDepositDetails3);

        fixedDepositService.getMoney("62221002",2000.0f);

        FixedDepositDetails fixedDepositDetails4 = fixedDepositService.queryDepositById("62221002");

        System.out.println(fixedDepositDetails4);

    }


}
